package com.stx.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.stx.entity.Department;
import com.stx.jdbc.JdbcUtil;
import com.stx.jdbc.StringUtil;

/**
 * Servlet implementation class editdservlet
 */
public class editdservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public editdservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String did=request.getParameter("did");
		System.out.println(did);
		Connection con = null;
		PreparedStatement pst = null;
		Department department = new Department();
		try {
			con = JdbcUtil.getCon();
			pst = con.prepareStatement("select dname,daddress,dphone from tbl_department where did=?");
			pst.setString(1, did);
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				department.setDname(rs.getString("dname"));
				department.setDaddress(rs.getString("daddress"));
				department.setDphone(rs.getString("dphone"));
			}
			System.out.println(department.getDaddress());
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.closeCon(con, pst);
		}
		request.setAttribute("did", did);
		request.setAttribute("department", department);
		request.getRequestDispatcher("/admin/editd.jsp").forward(request, response);
	}

}
